package com.woniu.controller;

import com.woniu.entity.Employee;
import com.woniu.service.EmployeeService;
import com.woniu.util.ResponseUtil;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/Employee")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;

    /**
     * 根据动态条件查询员工列表.
     * 如果不提供任何参数，则查询所有员工.
     * 示例URL: /Employee/search?name=李&deptId=1
     */
    @GetMapping("/search")
    public ResponseUtil search(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String gender,
            @RequestParam(required = false) Integer deptId,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate
    ) {
        return employeeService.findWithConditions(name, gender, deptId, beginDate, endDate);
    }

    /**
     * 根据ID查询单个员工
     */
    @GetMapping("/findByEmployeeId/{employeeId}")
    public ResponseUtil findByEmployeeId(@PathVariable Integer employeeId){
        return employeeService.findByEmployeeId(employeeId);
    }

    /**
     * 添加员工
     */
    @PostMapping("/add")
    public ResponseUtil add(@RequestBody Employee employee){
        return employeeService.add(employee);
    }

    /**
     * 更新员工信息 (也可用于锁定/解锁员工)
     */
    @PutMapping("/update")
    public ResponseUtil update(@RequestBody Employee employee){
        return employeeService.update(employee);
    }

    /**
     * 删除员工
     */
    @DeleteMapping("/delete/{employeeId}")
    public ResponseUtil delete(@PathVariable Integer employeeId){
        return employeeService.delete(employeeId);
    }
}